<?php
/**
 * Created by PhpStorm.
 * User: WangXiaoHong
 * Date: 15-10-22
 * Time: 下午3:16
 */
if (!defined('IN_WECHAT') && !isset($_SESSION['sid'])) {
    exit('{"status":"fail","msg":"未登录或登录状态已过期","code":0}');
}
if(!isset($_REQUEST['action'])) exit('{"status":"fail","msg":"参数错误"}');
use \PFT\Db;
include_once '/var/www/html/new/d/module/common/Db.class.php';
include_once '/var/www/html/new/d/common/func.inc.php';
$dbConf = include_once '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息
Db::Conf($dbConf['remote_1']);
switch(safetxt(trim($_REQUEST['action'])))
{
    //发布公告
    case 'add':
        if(!isset($_REQUEST['title'])||!isset($_REQUEST['content'])) {
            Db::Close();
            exit('{"status":"fail","msg":"参数错误"}');
        }
        $status  = $_REQUEST['status']+0;
        $title   = trim($_REQUEST['title']);
        $content = trim($_REQUEST['content']);
        if(empty($title)||empty($content)) {
            Db::Close();
            exit('{"status":"fail","msg":"参数错误"}');
        }
        if(is_numeric(safetxt($_REQUEST['id']))){
            $sql="update pft_announce set update_time=now(),title=?,details=?,status=? where id=? limit 1";
            $stmt = Db::Connect()->prepare($sql);
            $stmt->execute(array($title,$content,$status,$_REQUEST['id']+0));
        }else{
            $sql="insert into pft_announce (type,title,details,status,creater,down_reason,create_time,update_time)
                  values (0,?,?,?,{$_SESSION['sid']},0,now(),now()) ";
            $stmt = Db::Connect()->prepare($sql);
            $stmt->execute(array($title,$content,$status));
        }
        if($stmt->rowCount()) {
            echo json_encode(array('status'=>'success','msg'=>'添加成功'));
        }else{
            echo json_encode(array('status'=>'fail','msg'=>'添加失败'));
        }
        break;
    //编辑公告
    case 'edit':
        if(!isset ($_REQUEST['id'])) {
            Db::Close();
            exit('{"status":"fail","msg":"参数错误"}');
        }
        $sql_edit="select title,details,id from pft_announce where id=? and status in(0,1) limit 1";
        $stmt = Db::Connect()->prepare($sql_edit);
        $stmt->execute(array($_REQUEST['id']+0));
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        if(empty($result)) {
            Db::Close();
            exit('{"status":"fail","msg":"无相关数据","code":"1"}');
        }
        echo json_encode($result);
        break;
    //移至草稿箱或发布
    case 'drafts':
        if(!isset ($_REQUEST['status'])||!isset ($_REQUEST['id'])) {
            Db::Close();
            exit('{"status":"fail","msg":"参数错误"}');
        }
        $id      = $_REQUEST["id"]+0;
        $status  = !($_REQUEST['status'])+0;
        $sql = "update pft_announce set status=? where id=? limit 1";
        $stmt =  Db::Connect()->prepare($sql);
        $stmt->execute(array($status,$id));
        if($stmt->rowCount()){
            echo json_encode(array('status'=>'success','msg'=>'操作成功'));
        }else{
            echo json_encode(array('status'=>'success','msg'=>'操作失败'));
        }
        break;
    //删除
    case 'delete';
        if(!isset ($_REQUEST['id'])) {
            Db::Close();
            exit('{"status":"fail","msg":"参数错误"}');
        }
        /*status为4 表示删除*/
        $sql  = "update pft_announce set status=4 where id=? limit 1";
        $stmt =  Db::Connect()->prepare($sql);
        $stmt->execute(array($_REQUEST["id"]+0));
        if($stmt->rowCount()){
            echo json_encode(array('status'=>'success','msg'=>'删除成功'));
        }else{
            echo json_encode(array('status'=>'success','msg'=>'删除失败'));
        }
        break;
    //查看
    case 'examine';
        if(!isset ($_REQUEST['id'])) {
            Db::Close();
            exit('{"status":"fail","msg":"参数错误"}');
        }
        $sql  = "select id,title,details,creater,create_time from pft_announce where id=? and status in(0,1) limit 1";
        $stmt = Db::Connect()->prepare($sql);
        $stmt->execute(array($_REQUEST["id"]+0));
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        $result['details']=htmlspecialchars_decode($result['details']);
        $sql_total="select count(id) as c from pft_announce_remark where an_id=?";
        $stmt      = Db::Connect()->prepare($sql_total);
        $stmt->execute(array($_REQUEST["id"]+0));
        $result['remarks']=$stmt->fetchColumn();

        if(empty($result)) {
            echo json_encode(array('status'=>'fail','msg'=>'无相关数据','code'=>1));
        }else {
            echo json_encode($result);

        }
        break;
    //显示评论
    case 'remark':
        $pageSize    = (isset($_REQUEST['pageSize'])) ? $_REQUEST['pageSize']+0:5;
        $currentPage = (isset($_REQUEST['currentPage'])) ? $_REQUEST['currentPage']+0:1;
        $pStart      = ($currentPage-1) * $pageSize;
        $sql_remark="select id,an_id,mid,comment,create_time from pft_announce_remark where an_id=? order by
                     create_time desc limit $pStart,$pageSize";
        $stmt  = Db::Connect()->prepare($sql_remark);
        $stmt ->execute(array($_REQUEST["id"]+0));
        $mid=array();
        //敏感词的转换数组
        $arr_get=file_get_contents('/var/www/html/new/d/module/pft_announce/Sen_ w.txt');
        $arrtoa =explode(PHP_EOL,$arr_get);
        foreach($arrtoa as $akey =>$aval){
            $arrto=explode('|',$aval);
            $re[]=$arrto[0];
        }
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $result[] = $row;
            $mid[]    = $row['mid'];
        }   if(empty($result)) {
            Db::Close();
            exit('{"status":"fail","msg":"暂无评论"}');
        }
        $sql_total="select count(id) as c from pft_announce_remark  where an_id=?";
        $stmt      = Db::Connect()->prepare($sql_total);
        $stmt->execute(array($_REQUEST["id"]+0));
        $total    =$stmt->fetchColumn();
        $totalPage=ceil($total/$pageSize);
        Db::Conf($dbConf['localhost']);
        $sql_member="select dname,id from pft_member where id in(".implode(',',$mid).")";
        $mid=array();
        $stmt      = Db::Connect()->prepare($sql_member);
        $stmt->execute();
        while($row =$stmt->fetch(PDO::FETCH_ASSOC)){
            $dname_id[$row['id']] = $row['dname'];
        }
        foreach($result as $res_key=>$res_val){

            $result[$res_key]['dname']=$dname_id[$res_val['mid']];
            $result[$res_key]['comment'];
            foreach($re as $key_ret=>$val_ret) {
                $result[$res_key]['comment']=str_replace($val_ret,'***',$result[$res_key]['comment']);

            }
        }
        echo json_encode(array('currentPage'=>$currentPage,'status'=>'success','pageSize'=>$pageSize,
            'totalPage'=>$totalPage,'list'=>$result));
        break;

    //发表评论
    case 'comment':
        //敏感词的转换数组
        $arr_get=file_get_contents('/var/www/html/new/d/module/pft_announce/Sen_ w.txt');
        $arrtoa =explode(PHP_EOL,$arr_get);
        foreach($arrtoa as $akey =>$aval){
            $arrto=explode('|',$aval);
            $re[]=$arrto[0];
        }

        $id      = $_REQUEST["id"]+0;
        $comment = trim($_REQUEST['comment']);
        if(empty($comment)) {
            Db::Close();
            exit('{"status":"fail","msg":"请输入评论内容"}');
        }
        $thumb_up= isset($_REQUEST['thumb'])?$_REQUEST['thumb']+0:0;
        $sql_com = "insert into pft_announce_remark (an_id,mid,comment,thumb_up,create_time)
                    values (?,{$_SESSION['sid']},?,?,now()) ";
        $stmt =  Db::Connect()->prepare($sql_com);
        $stmt->execute(array($id,$comment,$thumb_up));
        if($stmt->rowCount()){
            $sid = Db::lastInsertId();
            $sql  = "select id,an_id,mid,thumb_up,comment,create_time from pft_announce_remark where id=$sid limit 1";
            $result = Db::fetch($sql);
            foreach($re as $key_ret=>$val_ret) {
                $result['comment']=str_replace($val_ret,'***',$result['comment']);
            }

            Db::Conf($dbConf['localhost']);
            $sql_member="select dname,id from pft_member where id={$result['mid']}  limit 1";
            $stmt      = Db::Connect()->prepare($sql_member);
            $stmt->execute();
            while($row =$stmt->fetch(PDO::FETCH_ASSOC)){
                $result['dname'] = $row['dname'];
            }
            echo json_encode(array('status'=>'success','msg'=>'评论成功','list'=>$result));
        }else{
            echo json_encode(array('status'=>'failure','msg'=>'评论失败'));
        }
        break;
//    case 'test':
//        $aa=$_GET['aa'];
//        $arr_get=file_get_contents('Sen_ w.txt');
//        $arrtoa =explode(PHP_EOL,$arr_get);
//        foreach($arrtoa as $akey =>$aval){
//            $arrto=explode('|',$aval);
//            $return[]=$arrto[0];
//        }
////        $retuen_flip=array_flip($return);
//        $aat=$aa;
//        foreach($return as $key_ret=>$val_ret) {
//
//            $aat=str_replace($val_ret,'**',$aat);
//
//        }
//        echo '<pre>';
//        print_r($aa);
//        print_r($aat);
//        print_r($return);
//        echo '</pre>';
//        break;
    default :
        echo json_encode(array("status"=>"fail","msg"=>"参数错误"));
        break;
}
Db::Close();